package com.mgl.movesprite.game.model;

import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.mgl.movesprite.EntityAnimatedSpriteLevelBase;
import com.mgl.movesprite.game.factory.GameObjectFactory;
import com.mgl.util.Path;
import com.mgl.util.Point;

public class Monster extends EntityAnimatedSpriteLevelBase {

	public Monster(float pX, float pY, ITiledTextureRegion pTiledTextureRegion,
			VertexBufferObjectManager pVertexBufferObjectManager,
			GameObjectFactory gameObjectFactory) {
		super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager,
				gameObjectFactory);
		// TODO Auto-generated constructor stub
		animate(new long[] { 100, 100, 100 }, 3, 5, true);

	}

	private String nameTexture;

	private float speed = 25;

	private Path path = null;
	private Point pointToMove = null;

	public void move(float pSecondsElapsed) {

	}

	public void moveFollowingPath(float pSecondsElapsed) {

		float x = this.getX();
		float y = this.getY();
		float distance = pSecondsElapsed * speed;

		Point currentPoint = new Point(x, y);

		if (pointToMove == null && path.isEmpty()) {
			return;
		}

		if (!path.isEmpty()) {
			pointToMove = path.peek();
		}

		float disAux = Point.distanceBetween(currentPoint, pointToMove);

		if (distance > disAux) {
			if (!path.isEmpty()) {
				pointToMove = path.pop();
			} else {
				this.setPosition(pointToMove.getX(), pointToMove.getY());
				pointToMove = null;
				return;
			}

		}

		float angle = Point.angleBetween(currentPoint, pointToMove);

		float dx = distance * (float) Math.cos(angle);
		float dy = distance * (float) Math.sin(angle);

		this.setPosition(x + dx, y + dy);

	}

	public void update(float pSecondsElapsed, Level level) {
		try {

			moveFollowingPath(pSecondsElapsed);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public String getNameTexture() {
		return nameTexture;
	}

	public void setNameTexture(String nameTexture) {
		this.nameTexture = nameTexture;
	}

	public float getSpeed() {
		return speed;
	}

	public void setSpeed(float speed) {
		this.speed = speed;
	}

	public Path getPath() {
		return path;
	}

	public void setPath(Path path) {
		this.path = path.clone();
	}

	public Point getPointToMove() {
		return pointToMove;
	}

	public void setPointToMove(Point pointToMove) {
		this.pointToMove = pointToMove;
	}

}
